# 导包
import requests


# 创建测试类
class TestLoginAPI:
    def setup(self):
        # 创建session对象
        self.session = requests.session()
        # 获取图片验证码
        url = "http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify"
        res_v = self.session.get(url=url)
        # 断言响应状态码
        assert res_v.status_code == 200
        # 断言响应头信息包含
        assert "image" in res_v.headers.get("Content-Type")

    # 创建测试方法
    # 登录成功
    def test01_login_success(self):
        url = "http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
        login_data = {
            "username": "13488888888",
            "password": "123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=url, data=login_data)
        # 断言响应状态码
        assert response.status_code == 200
        # 断言status值为1
        assert response.json().get("status") == 1
        # 断言msg值包含成功
        assert "成功" in response.json().get("msg")

    # 账号不存在
    def test02_mobile_error(self):
        url = "http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
        login_data = {
            "username": "13488888889",
            "password": "123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=url, data=login_data)
        # 断言响应状态码
        assert response.status_code == 200
        # 断言status值为-1
        assert response.json().get("status") == -1
        # 断言msg值包含不存在
        assert "不存在" in response.json().get("msg")

    # 密码错误
    def test03_password_error(self):
        url = "http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
        login_data = {
            "username": "13488888888",
            "password": "123446",
            "verify_code": "8888"
        }
        response = self.session.post(url=url, data=login_data)
        # 断言响应状态码
        assert response.status_code == 200
        # 断言status值为-2
        assert response.json().get("status") == -2
        # 断言msg值包含错误
        assert "错误" in response.json().get("msg")
